
package com.example.administrator.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/5.
 * Modify On 2019/10/10
 */


public class LineChartActivity2 extends DemoBase implements OnChartValueSelectedListener {
    private LineChart mChart;
  //  private Spinner mSp;
    private TextView mtv;

    public  String[] xData = new String[]{"14:00", "15:00","16:00", "17:00","18:00"};
    //public List<AxisValue>
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.layout_testbarchart);

        mChart = (LineChart) findViewById(R.id.gettestchart);
     //   mtv = findViewById(R.id.tv_time);
     //   mtv.setText("         8:00-9:00 10:00-11:00 12:00-13:00 14:00-15:00 16:00-17:00 18:00-19:00");
     //   mtv.setTextSize(10);

     //   mSp = findViewById(R.id.TestSpinner);

        mChart.setOnChartValueSelectedListener(this);

        // 没有描述的文本
        mChart.getDescription().setEnabled(false);

        // 支持触控手势
        mChart.setTouchEnabled(false);

        mChart.setEnabled(true); // 使能

        mChart.setDragDecelerationFrictionCoef(0.9f);

        // 支持缩放和拖动
        mChart.setDragEnabled(false);
        mChart.setScaleEnabled(false);
        mChart.setDrawGridBackground(false);
        mChart.setHighlightPerDragEnabled(false);

        // 如果禁用，可以在x轴和y轴上进行缩放
        mChart.setPinchZoom(true);

        // 设置另一种背景颜色
        mChart.setBackgroundColor(Color.WHITE);

        // 添加数据点个数

        mChart.animateXY(4000, 4000);  // 动画延迟

        Legend legend = mChart.getLegend();
        legend.setEnabled(false);



        XAxis xAxis = mChart.getXAxis(); // X轴上下
        xAxis.setEnabled(false);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);  //底端
        xAxis.setDrawAxisLine(false);  //第一条线
        xAxis.setDrawGridLines(false); // 设置 竖线消失
        xAxis.setDrawLabels(true);

       // xAxis.setValueFormatter();


        YAxis leftAxis = mChart.getAxisLeft();  // Y 左
        leftAxis.setEnabled(true);
        leftAxis.setDrawAxisLine(false); //第一条线
        leftAxis.setDrawGridLines(true);// 设置线
        leftAxis.setDrawLabels(false); // 设置标签


        YAxis rightAxis = mChart.getAxisRight(); // Y 右
        rightAxis.setEnabled(false);

        setData(6);

     //   init();
    }

   /* private void init(){

        List<String> ls =  new ArrayList<>();
        ls.add("数据一");
        ls.add("数据二");
        ls.add("数据三");

         ArrayAdapter<String>adapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,ls);
        adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
       mSp.setAdapter(adapter);
        mSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                switch (position){
                    case 0:
                        mChart.clear();    // 使用之后 在出现下一次数据时候要清除界面
                        mChart.invalidate(); //清理
                        setData(6);

                        break;
                    case 1:
                        mChart.clear();
                        mChart.invalidate();
                        setData(10);

                        break;
                    case 2:
                        mChart.clear();
                        mChart.invalidate();
                        setData(15);
                        break;

                        default:
                            break;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }*/
    // 添加进去一个坐标点
    private void addEntry(int count) {
        LineDataSet set ;
        // 生成随机测试数

        // set.getEntryCount()获得的是所有统计图表上的数据点总量，
        // 如从0开始一样的数组下标，那么不必多次一举的加1
//        for(int i = 0;i<count;i++) {
//            Entry entry = new Entry(count, f); // X  Y
//            data.addEntry(entry, 0);
//        }

        List<Entry> ts = new ArrayList<Entry>();
        for(int i =0;i<count;i++){
            float f = (float) ((Math.random()) * 20 + 30);
            ts.add(new Entry(i,f));
        }

        set = (LineDataSet) mChart.getData().getDataSetByIndex(0);  //从 0 开始设置数字
        set.setValues(ts);  //设置值

        mChart.notifyDataSetChanged();
        mChart.getData().notifyDataChanged();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return true;
    }

    private void setData(int count) {

        ArrayList<Entry> values = new ArrayList<Entry>();

        for (int i = 0; i < count; i++) {

            float f = (float) ((Math.random()) * 20 + 30);

            values.add(new Entry(i, f));
        }

        ArrayList<Entry> values2 = new ArrayList<Entry>();

        for (int i = 0; i < count; i++) {

            float f = (float) ((Math.random()) * 20 + 10);

            values2.add(new Entry(i, f));
        }





//        ArrayList<HashMap> arrayList = new ArrayList<>();
//
//        for (int i = 0; i < count; i++) {
//
//            float f = (float) ((Math.random()) * 20 + 30);
//
//            arrayList.add((HashMap) new HashMap().put("aaa",f));
//        }


        LineDataSet LineDataSet,LineDataSet2;

        if (mChart.getData() != null &&
                mChart.getData().getDataSetCount() > 0) {
            LineDataSet = (LineDataSet)mChart.getData().getDataSetByIndex(0);
            LineDataSet.setValues(values);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            // 创建一个数据集并给它一个类型
            // 设置
            LineDataSet = new LineDataSet(values, "DataSet 0");  //数据集合
            LineDataSet.setColor(getResources().getColor(R.color.colorPM2_5));  //线的颜色
            LineDataSet.setCircleColor(getResources().getColor(R.color.colorPM2_5));  //点的颜色
            LineDataSet.setValueTextSize(9); // 字体大小

            LineDataSet2 = new LineDataSet(values2,"DataSet 1");
            LineDataSet2.setColor(getResources().getColor(R.color.colorP10));  //线的颜色
            LineDataSet2.setCircleColor(getResources().getColor(R.color.colorP10));  //点的颜色
            LineDataSet2.setValueTextSize(9); // 字体大小

            // 用数据集创建一个数据对象



            List<ILineDataSet> dataSets = new ArrayList<>() ;

            dataSets.add(LineDataSet);
            dataSets.add(LineDataSet2);

            LineData lineData = new LineData(dataSets);

            lineData.setValueTextColor(Color.BLACK);


            // 设置
            mChart.setData(lineData);
        }
    }



    @Override
    public void onValueSelected(Entry e, Highlight h) {

    }

    @Override
    public void onNothingSelected() {

    }

}
